package com.kamloo.stg.dialog;

import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import com.kamloo.stg.data.db.Period;
import com.kamloo.stg.data.db.PeriodDAO;
import com.kamloo.stg.fragment.TabBookmarksSchedule;
import com.kamloo.stg.info.AppConstant;
import com.kamloo.stg.R;

public class TabBookmarkPeriodDialog extends DialogFragment {
	// ----- GLOBAL VARIABLE -----
	private int m_nodeId;
	private ArrayList<Period> m_periods;

	// ----- LISTENER -----
	
	OnClickListener radioButtonClickListener = new OnClickListener() {
		public void onClick(View view) {
			boolean checked = ((RadioButton) view).isChecked();
			FragmentManager manager = getActivity().getSupportFragmentManager();

			FragmentTransaction ft = manager.beginTransaction();
			// Create and replace the fragment that represent the tabs content
			Fragment newFragment = Fragment.instantiate(getActivity(), TabBookmarksSchedule.class.getName(), null);
			ft.replace(R.id.realtabcontent, newFragment, AppConstant.FRAGMENT_TAB_BOOKMARKS_SCHEDULE);

			// Back button and smooth transition feeling
			ft.addToBackStack(null);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

			// Commit and execute
			ft.commit();
			manager.executePendingTransactions();

			// Check which radio button was clicked
			switch (view.getId()) {
			case R.id.tabBookmarkPeriodDialogChoiceA:
				if (checked) {
					TabBookmarksSchedule bookmarkSchedule = (TabBookmarksSchedule) getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_TAB_BOOKMARKS_SCHEDULE);
					bookmarkSchedule.initializeView(m_nodeId, m_periods.get(0).getId());
				}
				break;
			case R.id.tabBookmarkPeriodDialogChoiceB:
				if (checked) {
					TabBookmarksSchedule bookmarkSchedule = (TabBookmarksSchedule) getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_TAB_BOOKMARKS_SCHEDULE);
					bookmarkSchedule.initializeView(m_nodeId, m_periods.get(1).getId());
				}
				break;
			case R.id.tabBookmarkPeriodDialogChoiceC:
				if (checked) {
					TabBookmarksSchedule bookmarkSchedule = (TabBookmarksSchedule) getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_TAB_BOOKMARKS_SCHEDULE);
					bookmarkSchedule.initializeView(m_nodeId, m_periods.get(2).getId());
				}
				break;
			case R.id.tabBookmarkPeriodDialogChoiceD:
				if (checked) {
					TabBookmarksSchedule bookmarkSchedule = (TabBookmarksSchedule) getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_TAB_BOOKMARKS_SCHEDULE);
					bookmarkSchedule.initializeView(m_nodeId, m_periods.get(3).getId());
				}
				break;
			}

			FragmentTransaction transaction = manager.beginTransaction();
			transaction.remove(getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_TAB_BOOKMARKS_PERIOD_DIALOG));
			transaction.commit();
			manager.executePendingTransactions();
		}
	};
	

	// ----- CLASS CODE -----
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = (LinearLayout) inflater.inflate(R.layout.tab_bookmarks_period_dialog, container, false);
		RadioButton periodA = (RadioButton) view.findViewById(R.id.tabBookmarkPeriodDialogChoiceA);
		RadioButton periodB = (RadioButton) view.findViewById(R.id.tabBookmarkPeriodDialogChoiceB);
		RadioButton periodC = (RadioButton) view.findViewById(R.id.tabBookmarkPeriodDialogChoiceC);
		RadioButton periodD = (RadioButton) view.findViewById(R.id.tabBookmarkPeriodDialogChoiceD);

		periodA.setOnClickListener(radioButtonClickListener);
		periodB.setOnClickListener(radioButtonClickListener);
		periodC.setOnClickListener(radioButtonClickListener);
		periodD.setOnClickListener(radioButtonClickListener);

		m_periods = getAllPeriod();
		getDialog().setTitle(R.string.tabBookmarks_PeriodDialogTitle);

		return view;
	}

	public void initializeView(int _noeudId) {
		this.m_nodeId = _noeudId;
	}

	
	private ArrayList<Period> getAllPeriod() {
		PeriodDAO periodDao = new PeriodDAO(getActivity());
    	periodDao.open();
    	
    	ArrayList<Period> periods = periodDao.selectAll();
    	periodDao.close();
    	return periods;
	}	
}
